package top.revery.admin.controller_admin;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.revery.admin.util.FeignServiceMapping;
import top.revery.api.entity.user.BaseUserInfo;
import top.revery.api.util.reveryTool;

import java.io.File;
import java.util.Date;

/**
 * <h1> ---  --- </h1>
 * Created by  revery.top jun
 * Date  2019-12-13 16:37
 */
@RestController
@RequestMapping("/admin/pull")
public class PullDataController {
    @GetMapping("/userList")
    void userList() throws Exception {
        String filePath = reveryTool.getRootPath(this)+"classes/config/user.json";

        File file = ResourceUtils.getFile(filePath);
        JSONArray jsonArray = JSON.parseArray(FileUtils.readFileToString(file));

        String time = String.valueOf(new Date().getTime()).substring(10);
        for(int i=0; i<jsonArray.size(); i++){
            JSONObject json = jsonArray.getJSONObject(i);

            BaseUserInfo baseUserInfo = new BaseUserInfo();
            baseUserInfo.setName(time+i);
            baseUserInfo.setNickName(json.getString("nick_name"));
            baseUserInfo.setPhoto(json.getString("avatar_uri"));
            baseUserInfo.setPassword(String.valueOf((int)(Math.random()*9999999)));

            System.out.println(FeignServiceMapping.uDataSource.save(baseUserInfo));;
        }
    }

    public static void main(String[] args) {
        try{
            PullDataController pullDataController = new PullDataController();
            pullDataController.userList();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
